PHP comments function Posted on 2017-08-20 | In PHP use PHP jquery json mysql to build comments function commentsindex.html1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>comments</title> <style type="text/css"> .demo{ width:500px; margin: 0 auto; } h3{ font-size:18px } #comments{ margin:10px auto } #post{ margin-top:10px } #comments p,#post p{ line-height:30px } #comments p span{ margin:4px; color:#999 } #message{ position:relative; display:none; width:100px; padding:4px; margin-top:-100px; margin-left:30px; background: #ff0000; color: #c286ff; z-index:1001 } </style> <script src="http://cdn.bootcss.com/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ var comments = $("#comments"); $.getJSON("server.php",function(json){ $.each(json,function(index,array){ var txt = "<p><strong>"+array["user"]+"</strong>:"+array["comment"]+"<span>"+array["addtime"]+"</span></p>"; comments.append(txt); }); }); $("#add").click(function(){ var user = $("#user").val(); var txt = $("#txt").val(); $.ajax({ type: "POST", url: "comment.php", data: "user="+user+"&txt="+txt, success: function(msg){ if(msg==1){ var str = "<p><strong>"+user+"</strong>:"+txt+"<span>just now</span></p>"; comments.append(str); $("#message").show().html("go!").fadeOut(1000); $("#txt").attr("value",""); }else{ $("#message").show().html(msg).fadeOut(1000); } } }); }); }); </script></head><body><div class="demo"> <div id="comments"> <h3>comments list</h3> </div> <div id="post"> <h3>comments</h3> <p>username</p> <p><input type="text" class="input" id="user" /></p> <p>contents</p> <p><textarea class="input" id="txt" style="width:100%; height:80px"></textarea></p> <p><input type="submit" value="submit" id="add" /></p> <div id="message"></div> </div></div></body></html> create comments database12345678910111213141516171819202122<?phpheader("Content-type:text/html;charset=utf-8"); $servername = "localhost";$username = "root";$password = "root";// connect$conn = mysqli_connect($servername, $username, $password);mysqli_set_charset($conn,'utf8'); //utf-8// check if (!$conn) { die("error " . mysqli_connect_error());}// create database$sql = "CREATE DATABASE comments";if (mysqli_query($conn, $sql)) { echo "s";} else { echo "e " . mysqli_error($conn);}mysqli_close($conn);?> 12345678910111213$sql = "CREATE TABLE `comments` ( `id` int(11) NOT NULL auto_increment, `user` varchar(30) NOT NULL, `comment` varchar(200) NOT NULL, `addtime` datetime NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM; " ;if (mysqli_query($conn, $sql)) { echo "s";} else { echo "e " . mysqli_error($conn);}mysqli_close($conn); server.php1234567891011<?phpheader("Content-type:text/html;charset=utf-8"); $conn=mysqli_connect("localhost","root","root","comments");mysqli_set_charset($conn,"utf8");$sql="SELECT * from comments";$que=mysqli_query($conn,$sql);while($row=mysqli_fetch_array($que)){ $comments[] = array("id"=>$row[id],"user"=>$row[user],"comment"=>$row[comment],"addtime"=>$row[addtime]);}echo json_encode($comments);?> comment.php12345678910111213141516171819<?phpheader("Content-type:text/html;charset=utf-8"); $user = htmlspecialchars(trim($_POST['user']));$txt = htmlspecialchars(trim($_POST['txt']));$time = date("Y-m-d H:i:s");if(empty($user)){ echo "username null"; exit;}if(empty($txt)){ echo "contents null"; exit;}$conn=mysqli_connect("localhost","root","root","comments");mysqli_set_charset($conn,"utf8");$sql="insert into comments(user,comment,addtime)values('$user','$txt','$time')";$que=mysqli_query($conn,$sql);if($que) echo "1";?>